package org.example.dyscript.dynamicscript;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;

import java.util.Map;

public class AviatorRule implements Rule {

  Expression executeExpression;
  Expression evaluateExpression;

  String executeScript = "ali_score > 100 "
      + "&& wx_score < 450 "
      + "&& ! string.contains(pd_living_province, \"乌克兰\") "
      + "&& string.contains(pd_census_province, \"吉尔吉斯斯坦\")";

  String evaluateScript = "age > 18 && string.contains(vip, \"vip\")";

  public AviatorRule() {
    executeExpression = AviatorEvaluator.compile(executeScript);
    evaluateExpression = AviatorEvaluator.compile(evaluateScript);
  }

  @Override
  public boolean execute(Map<String, Object> parameters) {
    return (boolean) executeExpression.execute(parameters);
  }

  @Override
  public boolean evaluate(Map<String, Object> parameters) {
    return (boolean) evaluateExpression.execute(parameters);
  }
}
